<?php

namespace app\common\model;

use app\common\model\BaseModel;
use think\model\concern\SoftDelete;

class MerchantCar extends BaseModel
{

    use SoftDelete;

    

    // 表名
    protected $name = 'merchant_car';
    
    // 自动写入时间戳字段
    protected $autoWriteTimestamp = 'int';

    // 定义时间戳字段名
    protected $createTime = 'createtime';
    protected $updateTime = 'updatetime';
    protected $deleteTime = 'deletetime';

    // 追加属性
    protected $append = [

    ];

	//普通状态
	public function getStatusList($status = null)
	{
		$data = ['1' => __('上架'), '2' => __('下架')];
		if ($status){
			return $data[$status];
		}else{
			return $data;
		}
	}
	//审核状态
	public function getCheckStatusList($status = null)
	{
		$data = ['1' => __('待审核'), '2' => __('已通过'), '3' => __('已拒绝')];
		if ($status){
			return $data[$status];
		}else{
			return $data;
		}
	}
    //出租状态
    public function getCarStatusList($car_status = null)
    {
        $data = ['1' => __('闲置中'), '2' => __('出租中')];
        if ($car_status){
            return $data[$car_status];
        }else{
            return $data;
        }
    }
	public function car(){
		return $this->hasOne('car','id','car_id')->joinType('left');
	}
    public function merchant(){
        return $this->hasOne('merchant','id','merchant_id')->joinType('left');
    }
    public function cars(){
        return $this->hasOne('car','id','car_id')->bind(['car_name','car_image'])->joinType('left');
    }

    public function brand(){
        return $this->hasOne('car_cate','id','brand_id')->joinType('left');
    }

    public function model(){
        return $this->hasOne('car_cate','id','model_id')->joinType('left');
    }

    public function type(){
        return $this->hasOne('car_cate','id','type_id')->joinType('left');
    }




	//车辆信息
	public static function carinfo($uid){
		$allnum = 0;
		$usenum = 0;
		$surplus = 0;
		$carInfo = MerchantCar::field('total_stock,use_stock')->where(['merchant_id' => $uid ,'examine_status' => 2, 'is_delete' => 1])->select()->toArray();
		if (!empty($carInfo)){
			foreach ($carInfo as $key=>$val){
				$allnum += $val['total_stock'];
                $surplus += $val['use_stock'];
                $usenum += ($val['total_stock'] - $val['use_stock']);
			}
		}
		$total = [
			'allnum' => $allnum,
			'usenum' => $usenum,
			'surplus' => $surplus,
		];
		return $total;
	}

    







}
